package com.getpebble.android.framework.util;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.TelephonyManager;
import com.getpebble.android.common.core.trace.Trace;
import com.google.common.base.Strings;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
import java.lang.reflect.Method;

public class PhoneUtil {
    private static final String TAG = PhoneUtil.class.getSimpleName();

    public static String getContactName(Context context, String phoneNumber) {
        boolean hasContactName = false;
        String str = phoneNumber;
        if (context == null) {
            Trace.info(TAG, "context is null, using phone number");
            return shortenPhoneNumber(phoneNumber);
        }
        Cursor c = null;
        try {
            Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
            c = context.getContentResolver().query(lookupUri, new String[]{"display_name"}, null, null, null);
            if (c != null && c.moveToNext()) {
                str = c.getString(c.getColumnIndexOrThrow("display_name"));
                hasContactName = true;
            }
            if (c != null) {
                c.close();
            }
        } catch (IllegalArgumentException iae) {
            Trace.warning(TAG, "Failed to retrieve contact info", iae);
            if (c != null) {
                c.close();
            }
        } catch (RuntimeException rte) {
            Trace.warning(TAG, "Failed to retrieve contact info", rte);
            if (c != null) {
                c.close();
            }
        } catch (Exception e) {
            Trace.warning(TAG, "Failed to retrieve contact info", e);
            if (c != null) {
                c.close();
            }
        } catch (Throwable th) {
            if (c != null) {
                c.close();
            }
        }
        if (!hasContactName) {
            str = shortenPhoneNumber(phoneNumber);
        }
        return str;
    }

    private static String shortenPhoneNumber(String phoneNumber) {
        String result = phoneNumber;
        if (Strings.isNullOrEmpty(phoneNumber)) {
            return "";
        }
        try {
            PhoneNumberUtil u = PhoneNumberUtil.getInstance();
            result = u.format(u.parse(phoneNumber, "US"), PhoneNumberFormat.NATIONAL);
        } catch (NumberParseException e) {
            Trace.warning(TAG, "Failed to parse phone number");
        }
        return result;
    }

    public static boolean endCall(Context context) {
        return callITelephonyMethod(context, "endCall");
    }

    private static boolean callITelephonyMethod(Context context, String method) {
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
            Method getITelephony = Class.forName(telephonyManager.getClass().getName()).getDeclaredMethod("getITelephony", new Class[0]);
            getITelephony.setAccessible(true);
            Object telephonyInterface = getITelephony.invoke(telephonyManager, new Object[0]);
            Class.forName(telephonyInterface.getClass().getName()).getDeclaredMethod(method, new Class[0]).invoke(telephonyInterface, new Object[0]);
            return true;
        } catch (Exception e) {
            Trace.debug(TAG, "Error calling ITelephony method: " + method, e);
            return false;
        }
    }
}
